<?php

namespace app\sales\controller;

use app\admin\common\Base;
use think\Db;
//销售明细表
class Salesdetail extends Base
{
   public function index()
   {
      $data = $this->request->param();
      //halt($data);
      $info = $data;
      if (isset($info['doubleclick']) && $info['doubleclick'] == 1) {
         $info['store_name'] = Db::name('store')->where('id', $info['store_id'])->where('is_del', 0)->value('name');
      } else {
         $info['doubleclick'] = 0;
      }
      $month = $this->get_month_start_to_end();
      $this->assign('month', $month);
      //销售人员
      $staff = Db::name('auth_user')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->where('status', 0)->order('id desc')->select();
      //halt( $staff);  
      $this->assign('staff', $staff);

      if (isset($data['ajax']) && $data['ajax'] == 100) {
         $page = isset($data['page']) ? $data['page'] : 1;
         $page = intval($page);
         $limit = isset($data['rows']) ? $data['rows'] : 10;
         $limit = intval($limit);
         $start = $limit * ($page - 1);

         if (isset($data['startdate']) && isset($data['enddate'])) {
            $month = $this->get_month_start_to_end($data['startdate'], $data['enddate']);
         } else {
            $month = $this->get_month_start_to_end();
         }
         $sql = Db::name('order_good')->alias('a')->join('order b', 'a.order_id = b.id')->field('a.*,b.*,a.mark as a_mark,b.mark as b_mark,a.id as a_id,b.id as b_id')->where('o_type', 'in', [1, 2])->where('b.addtime', '>=', $month['month_start'])->where('b.addtime', '<=', $month['month_end'])->where(['b.is_del' => 0, 'b.enterprise_id' => $this->EnterpriseId]);
         $sql = !isset($data['user_id']) || $data['user_id'] == '' ? $sql : $sql->where('b.user_id', $data['user_id']); //客户
         $sql = !isset($data['good_id']) || $data['good_id'] == '' ? $sql : $sql->where('a.good_id', $data['good_id']); //商品
         $sql = !isset($data['staff_id']) || $data['staff_id'] == '' ? $sql : $sql->where('b.by_admin', 'in', $data['staff_id']); //销售人员
         $sql = !isset($data['store_id']) || $data['store_id'] == '' ? $sql : $sql->where('a.store_id', 'in', $data['store_id']); //仓库

         $sql = !isset($data['o_sn']) || $data['o_sn'] == '' ? $sql : $sql->where('b.sn', $data['o_sn']); //单据号
         $sql = !isset($data['o_xhdd_sn']) || $data['o_xhdd_sn'] == '' ? $sql : $sql->where('b.xhdd_sn', $data['o_xhdd_sn']); //源订单号
         $sql = !isset($data['a_mark']) || $data['a_mark'] == '' ? $sql : $sql->where('a.mark', 'like', '%' . $data['a_mark'] . '%'); //分录备注
         $sql = !isset($data['b_mark']) || $data['b_mark'] == '' ? $sql : $sql->where('b.mark', 'like', '%' . $data['b_mark'] . '%'); //整单备注

         if (!isset($data['customer_category_id']) || $data['customer_category_id'] == 0 || $data['customer_category_id'] == '') {
         } else {
            $c = Db::name('customer')->where('category_id', $data['customer_category_id'])->select();
            $c['ids'] = implode(',', array_column($c, 'id'));
            $sql->where('b.user_id',  $c['ids']); //客户类别
         }
         if (!isset($data['goods_category_id']) || $data['goods_category_id'] == 0 || $data['goods_category_id'] == '') {
         } else {
            $c = Db::name('goods')->where('goods_category_id', $data['goods_category_id'])->select();
            $c['ids'] = implode(',', array_column($c, 'id'));
            $sql->where('a.good_id',  $c['ids']); //商品类别
         }

         $list0 = $sql->order('b.addtime desc,a.id asc')->select();
         $count = count($list0);
         $list = array_slice($list0, $start, $limit);

         foreach ($list as $key => &$value) {

            $goods = Db::name('goods')->where('id', $value['good_id'])->find();
            $value['danweichengben'] = Db::name('goods_price_strategy')->where('goods_id', $goods['id'])->value('estimated_purchase_price'); //单位成本-商品预计采购价?不确定
            $value['xiaoshouchengben'] = $value['danweichengben']*$value['shuliang']; //销售成本=单位成本*数量?不确定
            $value['xiaoshoumaoli'] = $value['jine']- $value['xiaoshouchengben']; //销售毛利=金额-销售成本?不确定
            $value['maolilv'] = sprintf("%.2f",$value['xiaoshoumaoli']/$value['jine']);  //毛利率=毛利/金额*100?不确定

            if ($value['o_type'] == 2) {
               $value['shuliang'] = $value['shuliang'] * -1;
               $value['jine'] = $value['jine'] * -1;
               $value['tax_e'] = $value['tax_e'] * -1;
               $value['zk_e'] = $value['zk_e'] * -1;
               $value['total_price'] = $value['total_price'] * -1;
            }

            $value['type_name'] = $value['o_type'] == 1 ? '销货' : '退货';
           
            $value['goods_cate'] =  Db::name('goods_category')->where('id', $goods['goods_category_id'])->value('name');
            $value['goods_sn'] = $goods['sn'];
            $value['goods_name'] = $goods['name'];
            $value['goods_code'] = $goods['code'];
            $value['goods_model'] = $goods['model'];
            $value['unit_name'] = Db::name('unit')->where('id', $value['unit_id'])->value('name');
            $value['addtime'] = date('Y-m-d', $value['addtime']);
            $user = Db::name('customer')->where('id', $value['user_id'])->find();
            $value['user_name'] = $user['sn'] . ' ' . $user['name'];
            $value['user_cate_name'] = Db::name('customer_category')->where('id', $user['category_id'])->value('name');
            $value['store_name'] = Db::name('store')->where('id', $value['store_id'])->value('name');
            $value['dj_time'] = date('Y-m-d', $value['dj_time']);
            $value['by_admin_name'] = Db::name('auth_user')->where('id', $value['by_admin'])->value('name');

            $value['shenhe_time'] = date('Y-m-d', $value['shenhe_time']);


         }
         //halt($list);
         return $this->grid_data($list, $count, $page, $limit);
      }

      $customer = Db::name('customer')->where('is_customer', 'in', '1,3')->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
      $goods = Db::name('goods')->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
      $this->assign('info', $info);
      $this->assign('customer', $customer);
      $this->assign('goods', $goods);
      return $this->fetch();
   }
}
